package android.bluebox.view;

import android.app.Activity;
import android.bluebox.R;
import android.bluebox.model.DatabaseBox;
import android.bluebox.model.StaticBox;
import android.content.Intent;
import android.os.Bundle;

public class Main extends Activity {

	Intent intent;
	boolean isFirstTime = false;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		StaticBox.dbBox = new DatabaseBox(this);
		updateDatabase();
//		intent = new Intent(Main.this, FirstView.class);
		if (checkExistedFile()) {
			intent = new Intent(Main.this, LoginView.class);
		} else {
			intent = new Intent(Main.this, InitPasswordView.class);
			isFirstTime = true;
		}
		startActivityForResult(intent, 0);
	}

	public boolean checkExistedFile() {
		String hash = StaticBox.dbBox.getSystemHash();
		return (hash != null && !hash.equals(""));
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if(resultCode == RESULT_OK && isFirstTime) //all done
		{
			intent = new Intent(Main.this, LoginView.class);
			startActivity(intent);
		}
	}
	
	public void updateDatabase() {
		StaticBox.dbBox.updateSynonyms("password", "password passwd pass pwd loginpwd login[password] ");
		StaticBox.dbBox.updateSynonyms("username", "username login ");
		StaticBox.dbBox.updateSynonyms("email", "email mail login[email] ");
	}
}
